package com.haier.npt.filter;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.AntPathMatcher;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * @Description:url忽略
 * @author: lixu
 * @date:   2018年7月12日 上午10:33:49   
 */
public class IgnoreUrlFilter extends ZuulFilter{

	/**
	 * 放行的url
	 */
	private static List<String> permitUrls = new ArrayList<>();
	
	static{
		permitUrls.add("/**/user/login**"); //登录
		permitUrls.add("/**/user/checkName**"); //验证账号
		permitUrls.add("/**/code/**"); //验证码
		permitUrls.add("/**/demo/**/api-docs**");
	}
	
	/**
	 * @Description: 添加过滤的url
	 * @param: @param urlPattern      
	 * @return: void      
	 * @throws   
	 */
	protected void permitUrl(String urlPattern){
		permitUrls.add(urlPattern);
	}
	
	@Override
	public boolean shouldFilter() {
		return true;
	}
	
	@Override
	public Object run(){
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		String requestUri = request.getRequestURI();
		AntPathMatcher pathMatcher = new AntPathMatcher();
		for(String pattern : permitUrls){
			if(pathMatcher.match(pattern, requestUri)){
				ctx.set("ignore",Boolean.TRUE);
				return null;
			}
		}
		ctx.set("ignore",Boolean.FALSE);
		return null;
	}
	
	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 1;
	}
}
